﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using HKB.Common;
using HKB.Data.Bll;
using HKB.Web;
using Wuqi.Webdiyer;

/// <summary>
/// StaticFunApp 的摘要说明
/// </summary>
public static class StaticFunApp
{
    private static DataRow _ShopConfig = null;
    private static DataRow _SiteConfig = null;
    public static DataRow ShopConfig
    {
        get
        {
            if (StaticFunApp._ShopConfig == null)
            {
                object obj = DataCache.GetCache("shopconfig");
                if (obj == null)
                {
                    obj = new ShopConfigBll().GetConfig().Rows[0];
                    if (obj != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("config");
                        DataCache.SetCacheDepFile("shopconfig", obj, DataCache.shopconfigfile);
                    }
                }
                StaticFunApp._ShopConfig = (DataRow)obj;
            }
            return StaticFunApp._ShopConfig;
        }
    }
    public static DataRow SiteConfig
    {
        get
        {
            if (StaticFunApp._SiteConfig == null)
            {
                object obj = DataCache.GetCache("siteconfig");
                if (obj == null)
                {
                    obj = new SiteConfigBll().GetSiteConfig().Rows[0];
                    if (obj != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("config");
                        DataCache.SetCacheDepFile("siteconfig", obj, DataCache.siteconfigfile);
                    }
                }
                StaticFunApp._SiteConfig = (DataRow)obj;
            }
            return StaticFunApp._SiteConfig;
        }
    }
    public static void AspNetPage(int PageIndex, int PageSize, DataSet ds, GridView GridView1, AspNetPager AspNetPager1)
    {
        AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString());
        AspNetPager1.PageSize = PageSize;
        AspNetPager1.LastPageText = "尾页";
        AspNetPager1.FirstPageText = "首页";
        AspNetPager1.PrevPageText = "上一页";
        AspNetPager1.NextPageText = "下一页";
        AspNetPager1.ShowBoxThreshold = 15;
        AspNetPager1.ShowPageIndexBox = ShowPageIndexBox.Auto;
        AspNetPager1.PageIndexBoxType = PageIndexBoxType.DropDownList;
        AspNetPager1.ShowNavigationToolTip = true;
        AspNetPager1.UrlPaging = true;
        AspNetPager1.UrlPagingTarget = "_self";
        AspNetPager1.TextBeforePageIndexBox = "转到第";
        AspNetPager1.TextAfterPageIndexBox = "页";
        AspNetPager1.SubmitButtonText = "跳转";
        GridView1.DataSource = ds.Tables[0].DefaultView;
        GridView1.DataBind();
    }
    public static void AspNetPage(int PageIndex, int PageSize, DataSet ds, Repeater Repeater1, AspNetPager AspNetPager1)
    {
        AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString());
        AspNetPager1.PageSize = PageSize;
        AspNetPager1.LastPageText = "尾页";
        AspNetPager1.FirstPageText = "首页";
        AspNetPager1.PrevPageText = "上一页";
        AspNetPager1.NextPageText = "下一页";
        AspNetPager1.ShowBoxThreshold = 15;
        AspNetPager1.ShowPageIndexBox = ShowPageIndexBox.Auto;
        AspNetPager1.PageIndexBoxType = PageIndexBoxType.DropDownList;
        AspNetPager1.ShowNavigationToolTip = true;
        AspNetPager1.UrlPaging = true;
        AspNetPager1.UrlPagingTarget = "_self";
        AspNetPager1.TextBeforePageIndexBox = "转到第";
        AspNetPager1.TextAfterPageIndexBox = "页";
        AspNetPager1.SubmitButtonText = "跳转";
        Repeater1.DataSource = ds.Tables[0].DefaultView;
        Repeater1.DataBind();
    }
    public static void bindDataToRadioButtonList(this RadioButtonList rbl, string Dic_TableName, string Dic_FieldName, bool isNeedSelect)
    {
        DataTable dataTable = Dictionary.instance.GetDataByTable_Field(Dic_TableName, Dic_FieldName).Tables[0];
        if (dataTable != null && rbl != null)
        {
            if (dataTable.Columns.Count == 0)
            {
                dataTable.Columns.Add("FieldValue");
                dataTable.Columns.Add("FieldText");
            }
            else
            {
                if (dataTable.Rows.Count > 0)
                {
                    string text = dataTable.Rows[0]["Title"].ToString();
                }
            }
            rbl.DataSource = dataTable;
            rbl.DataTextField = "FieldText";
            rbl.DataValueField = "FieldValue";
            rbl.DataBind();
            if (isNeedSelect)
            {
                if (rbl.Items.Count > 0)
                {
                    rbl.Items[0].Selected = true;
                }
            }
        }
    }
    public static void bindDataToDropDownList(this DropDownList ddl, string Dic_TableName, string Dic_FieldName, bool isNeedShowEmpty, bool isNeedShowAll)
    {
        DataTable dataTable = Dictionary.instance.GetDataByTable_Field(Dic_TableName, Dic_FieldName).Tables[0];
        if (dataTable != null && ddl != null)
        {
            string text = "";
            if (dataTable.Columns.Count == 0)
            {
                dataTable.Columns.Add("FieldValue");
                dataTable.Columns.Add("FieldText");
            }
            else
            {
                if (dataTable.Rows.Count > 0)
                {
                    text = dataTable.Rows[0]["Title"].ToString();
                }
            }
            if (isNeedShowEmpty)
            {
                DataRow dataRow = dataTable.NewRow();
                dataRow["FieldValue"] = "";
                dataRow["FieldText"] = string.Format("--请选择--", text);
                dataTable.Rows.InsertAt(dataRow, 0);
            }
            else
            {
                if (isNeedShowAll)
                {
                    DataRow dataRow = dataTable.NewRow();
                    dataRow["FieldValue"] = "";
                    dataRow["FieldText"] = string.Format("--{0}--", (text == "") ? "全部" : text);
                    dataTable.Rows.InsertAt(dataRow, 0);
                }
                else
                {
                    DataRow dataRow = dataTable.NewRow();
                    dataRow["FieldValue"] = "";
                    dataRow["FieldText"] = "全部";
                    dataTable.Rows.InsertAt(dataRow, 0);
                }
            }
            ddl.DataSource = dataTable;
            ddl.DataTextField = "FieldText";
            ddl.DataValueField = "FieldValue";
            ddl.DataBind();
        }
    }
    public static void bindTableToDropDownList(this DropDownList ddl, DataTable dataTable, bool isNeedShowEmpty, bool isNeedShowAll)
    {
        if (dataTable != null && ddl != null)
        {
            string text = "";
            if (dataTable.Columns.Count == 0)
            {
                dataTable.Columns.Add("FieldValue");
                dataTable.Columns.Add("FieldText");
            }
            if (isNeedShowEmpty)
            {
                DataRow dataRow = dataTable.NewRow();
                dataRow["FieldValue"] = "";
                dataRow["FieldText"] = string.Format("--请选择--", text);
                dataTable.Rows.InsertAt(dataRow, 0);
            }
            else
            {
                if (isNeedShowAll)
                {
                    DataRow dataRow = dataTable.NewRow();
                    dataRow["FieldValue"] = "";
                    dataRow["FieldText"] = string.Format("--{0}--", (text == "") ? "全部" : text);
                    dataTable.Rows.InsertAt(dataRow, 0);
                }
                else
                {
                    DataRow dataRow = dataTable.NewRow();
                    dataRow["FieldValue"] = "";
                    dataRow["FieldText"] = "全部";
                    dataTable.Rows.InsertAt(dataRow, 0);
                }
            }
            ddl.DataSource = dataTable;
            ddl.DataTextField = "FieldText";
            ddl.DataValueField = "FieldValue";
            ddl.DataBind();
        }
    }
    public static void ddlSelect(this DropDownList ddl, string selectValue)
    {
        try
        {
            for (int i = 0; i < ddl.Items.Count; i++)
            {
                ddl.Items[i].Selected = false;
            }
            for (int i = 0; i < ddl.Items.Count; i++)
            {
                if (ddl.Items[i].Value == selectValue)
                {
                    ddl.Items[i].Selected = true;
                    break;
                }
            }
        }
        catch
        {
        }
    }
    public static void rblSelect(this RadioButtonList rbl, string selectvalue)
    {
        try
        {
            for (int i = 0; i < rbl.Items.Count; i++)
            {
                if (rbl.Items[i].Value == selectvalue)
                {
                    rbl.Items[i].Selected = true;
                }
            }
        }
        catch
        {
        }
    }
    public static string cblSelectValue(this CheckBoxList cbl)
    {
        string text = "";
        try
        {
            for (int i = 0; i < cbl.Items.Count; i++)
            {
                if (cbl.Items[i].Selected)
                {
                    text = text + cbl.Items[i].Value + ",";
                }
            }
            if (text != "")
            {
                text = text.Substring(0, text.Length - 1);
            }
        }
        catch
        {
        }
        return text;
    }
    public static void cblSelect(this CheckBoxList cbl, string value)
    {
        try
        {
            if (value.Contains(","))
            {
                string[] array = value.Split(new string[]
				{
					","
				}, StringSplitOptions.None);
                for (int i = 0; i < array.Length; i++)
                {
                    for (int j = 0; j < cbl.Items.Count; j++)
                    {
                        if (cbl.Items[j].Value == array[i])
                        {
                            cbl.Items[j].Selected = true;
                            break;
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    if (cbl.Items[i].Value == value)
                    {
                        cbl.Items[i].Selected = true;
                        break;
                    }
                }
            }
        }
        catch
        {
        }
    }
    public static string GetSelectValue(this DropDownList ddl, params string[] defaultvalue)
    {
        string result;
        if (ddl == null || ddl.Items.Count <= 0 || ddl.SelectedIndex < 0)
        {
            result = "";
        }
        else
        {
            if (defaultvalue.Length > 0 && ddl.SelectedValue == defaultvalue[0])
            {
                result = "";
            }
            else
            {
                result = ddl.SelectedValue.IsNull(new object[0]);
            }
        }
        return result;
    }
    public static string GetSelectText(this DropDownList ddl, params string[] defaultvalue)
    {
        string result;
        if (ddl == null || ddl.Items.Count <= 0 || ddl.SelectedIndex < 0)
        {
            result = "";
        }
        else
        {
            if (defaultvalue.Length > 0 && ddl.SelectedItem.Text == defaultvalue[0])
            {
                result = "";
            }
            else
            {
                result = ddl.SelectedItem.Text.IsNull(new object[0]);
            }
        }
        return result;
    }
    public static bool CreateSmallImageForAllGoods()
    {
        StaticFun.UpdateProductImage();
        Watermark watermark = new Watermark();
        int num = StaticFunApp.SiteConfig["SamllPic_DefaultWidth"].ToInt32(new int[0]);
        int num2 = StaticFunApp.SiteConfig["SamllPic_DefaultHieght"].ToInt32(new int[0]);
        if (num < 10)
        {
            num = 220;
        }
        if (num2 < 10)
        {
            num2 = 220;
        }
        DataTable dataTable = Conn.Query(" SELECT ProductID,productimageurl,productsmallimageurl FROM HLShop_Product hp", new IDbDataParameter[0]).Tables[0];
        Conn conn = new Conn();
        bool result;
        try
        {
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                if (dataTable.Rows[i]["productimageurl"] != DBNull.Value)
                {
                    string text = dataTable.Rows[i]["productimageurl"].IsNull(new object[0]);
                    string text2 = text.StartsWith("/") ? text : ("/" + text);
                    text2 = text2.Replace("_small", "");
                    string extension = Path.GetExtension(text);
                    string text3 = string.Format("{0}_small{1}", text2.TrimEnd(extension.ToArray<char>()), extension);
                    string path = HttpContext.Current.Server.MapPath(text3);
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    bool flag = watermark.CreateSmallPic(text2, text3, num, num2);
                    if (flag)
                    {
                        conn.Execute(string.Format("update HLShop_Product set productimageurl='{0}' where ProductID={1}", text3.TrimStart(new char[]
						{
							'/'
						}), dataTable.Rows[i]["ProductID"]));
                    }
                }
            }
        }
        catch
        {
            result = false;
            return result;
        }
        result = true;
        return result;
    }
    public static void WriteLine(this HttpResponse Response, params object[] content)
    {
        if (content.Length == 0)
        {
            Response.Write("<br>");
        }
        else
        {
            for (int i = 0; i < content.Length; i++)
            {
                object obj = content[i];
                Response.Write(obj.IsNull(new object[0]) + "<br>");
            }
        }
    }
}